var required = require('../plugs/guimain/required')
var userTimeRun = require('../funcs/loginTimeOut')
var mongoFunc = require('../funcs/mongoFunc')
var send = require('../funcs/sendData')
var mongodb = require('../plugs/mongose/mongoQuery')
var result = app => {
	var isEmptyStr = required.isEmptyStr
	var isRange = required.isRange
	var isEmptyObject = required.isEmptyObject
	// addCode
			// rkvXqHKmH Start
			
app.post('/test/add',(req, res) => { // test-新增
    
  var params = req.body
  var name = params.name // 名称
  var age = params.age // 年龄
  var key = params.key // 键
  var value = params.value // 值
    
  var where = {
      name,
      age,
      key,
      value}
    
    
    where.createTime = new Date()
    where.updateTime = new Date()
    
  var token =  params.token
  userTimeRun(token, function (userData) {
    if (userData) {
        where.userId = userData._id
    
    
  mongodb.save('test', where,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {
      
      send(res, true, data)
    }
    return
  })
    
    
         }
    }, res)
})
			// rkvXqHKmH end
			
			// HkGU6JwXE Start

app.get('/test/data',(req, res) => { // test-查询
  console.log(req.cookies)
  console.log(req.cookies['Admin-Token'])
  var params = req.query

  var where = {
      }



  var token =  params.token
  userTimeRun(token, function (userData) {
    if (userData) {


  mongodb.findOne('test', where,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {

      send(res, true, data)
    }
    return
  })


         }
    }, res)
})
			// HkGU6JwXE end

			// B1v7lRUX4 Start

app.get('/test/list',(req, res) => { // test-查询

  var params = req.query
  var start = params.start // 分页起始位置
    if(!start){
      start = 0
    }
  var limit = params.limit // 分页显示条数
    if(!limit){
      limit = 10
    }

  var where = {
      }

  var otherWhere = {limit:limit,skip:start,sort:{'createTime':-1}}

    otherWhere.limit = parseInt(limit)
    otherWhere.skip = parseInt(start)


  var token =  params.token
  userTimeRun(token, function (userData) {
    if (userData) {


  mongodb.where('test', where,otherWhere,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {

      mongodb.count('test', where, function (err, data2) {
          if (err) {
            console.log(err)
            send(res, false)
          }
          else {
            var result = {total: data2, list: data}
            send(res, true, result)
          }
        })
        return;

      send(res, true, data)
    }
    return
  })


         }
    }, res)
})
			// B1v7lRUX4 end

			// Sy7mgRLmN Start

app.post('/test/add',(req, res) => { // test-新增

  var params = req.body
  var name = params.name // 姓名
    if (isEmptyStr(name)) {
      send(res, false, '姓名不能为空')
      return
    }
  var type = params.type // 类型
  var age = params.age // 年纪
  var arr = params.arr // 数组类型测试
  var obj = params.obj // 对象类型测试
  var bol = params.bol // bool类型测试
  var date = params.date // 时间类型测试

  var where = {
      name,
      type,
      age,
      arr,
      obj,
      bol,
      date}


    where.createTime = new Date()
    where.updateTime = new Date()

  var token =  params.token
  userTimeRun(token, function (userData) {
    if (userData) {
        where.userId = userData._id

     var where2 = { $or:[{name}] }
         mongodb.findOne('test', where2,function(err, data) {
            if(err) {
              console.log(err)
              send(res, false)
              return
            }
            if(data){
                send(res, false,"输入重复")
                return
             }else{

  mongodb.save('test', where,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {

      send(res, true, data)
    }
    return
  })
    }})

         }
    }, res)
})
		// Sy7mgRLmN end

			// Hkwx8ymM4 Start

app.get('/test/list',(req, res) => { // test-查询

  var params = req.query
  var start = params.start // 分页起始位置
    if(!start){
      start = 0
    }
  var limit = params.limit // 分页显示条数
    if(!limit){
      limit = 10
    }

  var where = {
      }

  var otherWhere = {limit:limit,skip:start,sort:{'createTime':-1}}

    otherWhere.limit = parseInt(limit)
    otherWhere.skip = parseInt(start)


  var token =  params.token
  userTimeRun(token, function (userData) {
    if (userData) {


  mongodb.where('test', where,otherWhere,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {

      mongodb.count('test', where, function (err, data2) {
          if (err) {
            console.log(err)
            send(res, false)
          }
          else {
            var result = {total: data2, list: data}
            send(res, true, result)
          }
        })
        return;

      send(res, true, data)
    }
    return
  })


         }
    }, res)
})
			// Hkwx8ymM4 end

			// BkAnqShZN Start

app.get('/test/add',(req, res) => { // test-新增

  var params = req.query
  var name = params.name // 名称
  var key = params.key // 秘钥
  var value = params.value // 值

  var where = {
      name,
      key,
      value}


    where.createTime = new Date()
    where.updateTime = new Date()




  mongodb.save('test', where,function(err, data) {
    if(err) {
      console.log(err)
      send(res, false)
    } else {

      send(res, true, data)
    }
    return
  })


})
		// BkAnqShZN end









}
module.exports = result
